package org.demo.honor.demo

import spock.lang.Specification

class DemoSpec extends Specification {
    //增强方法: setupSpec()方法在setup()方法前运行,cleanupSpec()在cleanup()方法后运行
    //before setup
    def setupSpec() {
        println("setupSpec")
    }

    //setup
    def setup() {
        println("setup")
    }

    //cleanup
    def cleanup() {
        println("cleanup")
    }

    //after cleanup
    def cleanupSpec() {
        println("cleanupSpec")
    }

    //测试方法
    def "testMethod"() {
        println "hello world"
        expect: "期望结果"
        1 < 2
    }

    def "method1"() {
        expect: "期望结果"
        Math.max(1, 3) == 3
        Math.max(7, 3) == 7
        Math.max(0, 0) == 0
    }

    def "method2"() {
        given: "准备变量"
        def a = 1
        def b = 2

        expect: "期望结果"
        a < b
    }

    def "method3"() {
        given: "准备变量"
        def num1 = 10
        def num2 = 20

        when: "调用"
        def res1 = Math.abs(num1)
        def res2 = -Math.abs(num2)

        then: "期望结果"
        res1 == 10
        res2 == -20
    }

    def "method4"() {
        given: "准备变量"
        String str = "aaabbbcd"

        when: "调用方法"
        def res1 = str.contains(str1)
        def res2 = str.contains(str2)

        then: "期望结果"
        res1
        res2

        where: "批量测试数据"
        str1  | str2
        "aaa" | "bbb"
        "ab"  | "cd"
    }

    //控制台可以看到更加具体的报错信息,显示第几组数据测试失败
//    @Unroll
//    def "method5"() {
//        given: "准备变量"
//        String str = "aaabbbcd"
//
//        when: "调用方法"
//        def res1 = str.contains(str1)
//        def res2 = str.contains(str2)
//
//        then: "期望结果"
//        res1
//        res2
//
//        where:
//        str1   | str2
//        "aaa"  | "bbb"
//        "ab"   | "bc"
//        "ccdf" | "g"
//    }
}
